package com.hot12345.common.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HtmlUtil {
	
	private final Log log = LogFactory.getLog(HtmlUtil.class);
	
	private String html = null;
	private final int connectTimeout = 5000;
	private String title = null;
	private String domian = null;
	
	public boolean getPageInfo(String url) throws Exception{
		if(url==null || "".equals(url.trim())){
			log.error("URL为空");
			return false;
		}
		
		URL urlObj1 = null;
		try{
			urlObj1 = new URL(url);
		}catch(Exception e){
			log.error("Exception",e);
			return false;
		}
		if(urlObj1==null){
			log.error("加载页面信息，不正确的域名");
			return false;
		}

		if(urlObj1.getHost()!=null){
			String url1 = urlObj1.getHost();
//			int length = url1.length();
//			int lastPointIndex = url1.lastIndexOf(".");
//			String prefixUrl = url1.substring(0, lastPointIndex);
//			String primaryDomain = prefixUrl.substring(prefixUrl.lastIndexOf(".")+1, prefixUrl.length());
//			String suffixUrl = url1.substring(lastPointIndex,length);
//			domian = primaryDomain+suffixUrl;
			this.domian = getTopDomian(url1);
		}

		try {
		      StringBuffer sbfHtml = new StringBuffer();
		      URL urlObj = new URL(url);		      
		      
		      HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
		      conn.setRequestMethod("GET");  
		      conn.setDoOutput(true);  
		      conn.setRequestProperty("User-Agent", "directclient");  
		      conn.setConnectTimeout(connectTimeout);
		      conn.connect();
		      String encoding = getCharsetEncoding(url);
//		      log.debug("conn.getContentType()=" + conn.getContentType());
//		      encoding = conn.getContentType().replaceAll(".+?charset=",""); 
		      log.debug("encoding=" + encoding);
		      
		      InputStreamReader isr = new InputStreamReader(conn.getInputStream(), encoding);
		      BufferedReader br = new BufferedReader(isr);
		      String temp;
		      int i= 0;
		      while ((temp = br.readLine()) != null) {
		    	  sbfHtml.append(temp).append("\n");
		    	  i++;
//		    	  log.debug("i="+i);
		    	  if(i>100)break;//最多只读一百行内容
		      }
		      br.close();
		      isr.close();
		     this.html = sbfHtml.toString();
		     return true;
		    } catch (Exception e) {
		      log.error("加载页面信息错误："+e);
		      return false;
		    }
		    
		    
	}
	
	 /**
	  *
	  * @param html 页面html
	  * @return 获得网页标题
	  */
	 public String getTitle() {
		 if(this.title!=null){
			 return this.title;
		 }
		 
		 //先去掉换行符
		 Pattern p = Pattern.compile("\r|\n");  //\\s*|\t|\r|\n    
		 Matcher m = p.matcher(html);      
		 String strNoBlank = m.replaceAll("");    
         
		String regex;
		String title = "";
		final List<String> list = new ArrayList<String>();
		regex = "<title>.*?</title>";
		final Pattern pa = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);//Pattern.CASE_INSENSITIVE大小写不敏感
		final Matcher ma = pa.matcher(strNoBlank);
		while (ma.find()) {
			list.add(ma.group());
		}
		for (int i = 0; i < list.size(); i++) {
			title = title + list.get(i);
		}
		if (title != null) {//过滤掉title标签
			title = title.replaceAll("(?i)<title>", "");//"(?i)"表示大小写不敏感
			title = title.replaceAll("(?i)</title>", "");
		}
		this.title = (title==null?"":title.trim());
		log.debug("title=" + title.trim());
		return this.title;
	}
	 
	 public String getCharsetEncoding(String url) throws Exception{
		 String defaultEncoding = "utf-8";
	     URL urlObj = new URL(url);		 
		 HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
		 conn.setRequestMethod("GET");
		 conn.setDoOutput(true);
		 conn.setConnectTimeout(connectTimeout);
		 conn.setRequestProperty("User-Agent", "directclient");
		 conn.connect();

		 String contentType = conn.getContentType();
		 log.debug("contentType=" + contentType);
		 int index = conn.getContentType().indexOf("charset=");
		 log.debug("charset index="+index);
		 
		 if(index<1){//没有charset=
			
			 BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			 String str;
			 while( (str = br.readLine()) != null){
	            log.debug("str="+str.toUpperCase());
	            int i = str.toUpperCase().indexOf("<META HTTP-EQUIV=\"CONTENT-TYPE\"");
	            int n = str.toUpperCase().indexOf("<META CHARSET=");
	            log.debug("index="+i);
	            if(i>-1 || n>-1){
	            	log.debug("str=" +str);
	            	String str1 = str.replaceFirst("\"", "");
	            	String charsetString = str1.substring(str1.toUpperCase().indexOf("CHARSET="), str1.length());
	            	int endIndex = charsetString.indexOf("\"");
	            	String charset = charsetString.substring(0, endIndex);
	            	if(charset==null || "".equals(charset.trim())){
	            		log.info("not found charset ,use default encoding");
	            		return defaultEncoding;
	            	}
//	            	String[] aa = charset.split("=");
	            	return charset.split("=")[1];
	            }
	           
	        }
	        br.close();
		 }
		 String encoding = conn.getContentType().replaceAll(".+?charset=", "");
		 log.debug("encoding=" + encoding);
		return encoding;
	 }

	public String getHtml() {
		return html;
	}

	public void setHtml(String html) {
		this.html = html;
	}

	public String getDomian() {
		return domian;
	}

	public void setDomian(String domian) {
		this.domian = domian;
	}
	
	private String getTopDomian(String url){
		String domainSuffix = ReadProperties.readValueBySystem("domainSuffix");
		if(domainSuffix==null || "".equals(domainSuffix.trim())){
			log.error("获取域名后缀异常");
			return null;
		}
//		Pattern p = Pattern.compile("(?<=http://|\\.)[^.]*?\\.(com.cn|cn|net|org|biz|info|cc|tv)",Pattern.CASE_INSENSITIVE);
		Pattern p = Pattern.compile(domainSuffix,Pattern.CASE_INSENSITIVE);
		Matcher matcher = p.matcher(url);
		matcher.find();
		return matcher.group();
	}
}
